package com.itestor.iptv.modules.ne.service.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import com.itestor.iptv.modules.ne.dao.IptvQualityDao;
import com.itestor.iptv.modules.ne.domain.IptvQuality;
import com.itestor.iptv.modules.ne.domain.RobotManage;
import com.itestor.iptv.modules.ne.service.IptvQualityService;
import com.itestor.iptv.modules.ne.vo.IptvQualityRateBean;
import com.itestor.iptv.modules.ne.vo.PictureData;
import com.itestor.iptv.utils.JdbcUtil;
/**
 * 服务可用性service
 * @author masaki
 */
public class IptvQualityServiceImpl implements IptvQualityService{
    
    /**
     * 
     */
    private static final Logger logger =Logger.getLogger(IptvQualityServiceImpl.class);
	/**
	 * iptvQualityDao
	 */
	private IptvQualityDao iptvQualityDao;
	/**
	 * @param iptvQualityDao
	 */
	public void setIptvQualityDao(IptvQualityDao iptvQualityDao) {
		this.iptvQualityDao = iptvQualityDao;
	}

	/**
	 * 获得所有的服务可用性实例
	 */
	@SuppressWarnings("unchecked")
	public List<IptvQuality> getAllIptvQuality() {
		Session session = iptvQualityDao.getHibernateSession();
		return session.createQuery("from IptvQuality").list();
	}
	/**
	 * 通过ids获得所需要的对象，绘图用
	 */
	@SuppressWarnings("unchecked")
	public List<Object[]> getIptvQualityByIds(Long[] ids) {
		Session session = iptvQualityDao.getHibernateSession();
		List list = session.createQuery("select ilr.idRobot,rm.name,ilr.errorCount,ilr.tatolCount,ilr.logTime from IptvQuality ilr,RobotManage rm where ilr.idRobot = rm.idRobot and ilr.idRobot in(:ids) order by ilr.idRobot , ilr.logTime").setParameterList("ids", ids).list();
		return list;
	}
	public List<Object[]> getIptvQualityByIdsAndTime(Long[] ids,Long startTime, Long endTime) {
		Session session = iptvQualityDao.getHibernateSession();
		List list = session.createQuery("select ilr.idRobot,rm.name,ilr.errorCount,ilr.tatolCount,ilr.logTime from IptvQuality ilr,RobotManage rm where ilr.idRobot = rm.idRobot and ilr.idRobot in(:ids) and ilr.logTime >= :startTime and ilr.logTime <= :endTime order by ilr.idRobot , ilr.logTime").setParameterList("ids", ids).setParameter("startTime", startTime).setParameter("endTime", endTime).list();
		return list;
	}
	
	
	/**
     * 获取服务可用性曲线数据
     * @param ids
     * @param startTime
     * @param endTime
     * @return List
     */
    public List<IptvQualityRateBean> getIptvQualityBeanByIdsAndTime(Integer ids[],Long startTime,Long endTime,Integer statunitsec){       
        List<IptvQualityRateBean>  beanList =new ArrayList<IptvQualityRateBean>();        
        Session session = iptvQualityDao.getHibernateSession();
        List<RobotManage> list = session.createQuery("from RobotManage rm where rm.idRobot in(:ids)").setParameterList("ids", ids).list();
        
        for(RobotManage vo : list){
            IptvQualityRateBean bean =new IptvQualityRateBean();  
            List<PictureData> dataList= getIptvQualityByIdAndTime(vo.getIdRobot(),startTime,endTime,statunitsec);
            bean.setDataList(dataList);
            bean.setIdRobot(vo.getIdRobot());
            bean.setName(vo.getName());
            beanList.add(bean);
        }
        
        return beanList;
    }
	
	/**
	 * 获取服务可用性曲线数据
	 * @param id
	 * @param startTime
	 * @param endTime
	 * @param statunitsec
	 * @return List
	 */
	public List<PictureData> getIptvQualityByIdAndTime(Integer id,Long startTime,Long endTime,Integer statunitsec){
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.US);
        
        StringBuffer sql = new StringBuffer();
        sql.append("{call pr_iptv_histroyquality(?,?,?,?)}");
        Session session = iptvQualityDao.getHibernateSession();
        Connection conn = session.connection();
        CallableStatement callstmt = null;
        ResultSet rs = null;
        
        List<PictureData> pictureDataList = new ArrayList<PictureData>();
        try{
            callstmt = conn.prepareCall(sql.toString());
            callstmt.setInt(1, id.intValue());
            String start_time =sdf.format(new Date(startTime*1000L));
            String end_time =sdf.format(new Date(endTime*1000L));
            
            logger.info("getIptvQualityByIdAndTime   id :" +id +" start_time :" +start_time +"end_time " +end_time +" statunitsec" +statunitsec);
            
            callstmt.setString(2, start_time);
            callstmt.setString(3, end_time);            
            callstmt.setInt(4, statunitsec);
            
            rs = callstmt.executeQuery();
            while(rs.next()){
                PictureData pictureData = new PictureData();
                pictureData.setRate(rs.getDouble("Quality"));
                pictureData.setTime(rs.getString("LogTime"));
                
                if (rs.getDouble("Quality")<99){
                    pictureData.setRate(99D);
                }
                
                pictureDataList.add(pictureData);
            }
        } catch (SQLException e) {           
            e.printStackTrace();
        }finally{
            JdbcUtil.close(rs,callstmt, conn);
        }
        return pictureDataList;
    }
	
}
